package com.letv.android.client.view;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

/**
 * 跑马灯TextView
 * 
 * @author zhuqiao
 *
 */
public class ScrollTextView extends TextView implements Runnable {
    private int currentScrollX;// 当前滚动的位置
    private int stepX = 4;// 每次滚动的步伐速度
    private boolean isStop = false;
    private int textWidth;
    private String content;
    private final int REPEAT = 1;
    private int repeatCount = 0;

    public ScrollTextView(Context context) {
        this(context, null);
    }

    public ScrollTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init() {
        setClickable(true);
        setSingleLine(true);
        setEllipsize(TruncateAt.MARQUEE);
    }

    public void setData(String text) {
        if (this.getText().toString().equals(text)) {
            return;
        }
        clearValue();
        this.content = text;
        setText(content);
        if (TextUtils.isEmpty(content)) {
            clearValue();
            stopScroll();
            return;
        }
        startScroll();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        stopScroll();
        startScroll();
    }

    public void clearValue() {
        currentScrollX = 0;
        repeatCount = 0;
        init();
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, type);
        MeasureTextWidth();
    }

    @Override
    public void setVisibility(int visibility) {
        if (visibility == View.INVISIBLE || visibility == View.GONE) {
            stopScroll();
        } else {
            startScroll();
        }
        super.setVisibility(visibility);
    }

    @Override
    public void onScreenStateChanged(int screenState) {
        super.onScreenStateChanged(screenState);
        if (screenState == SCREEN_STATE_ON) {
            startScroll();
        } else {
            stopScroll();
        }
    }

    /**
     * 获取文字宽度
     */
    private void MeasureTextWidth() {
        Paint paint = this.getPaint();
        String str = this.getText().toString();
        textWidth = (int) paint.measureText(str);
    }

    @Override
    public void run() {
        // 文字宽度小于textView宽度时，不滚动
        if (textWidth < this.getMeasuredWidth()) {
            return;
        }
        if (isStop) {
            return;
        }
        if (this.isShown()) {
            currentScrollX += stepX;// 滚动速度
            scrollTo(currentScrollX, 0);
        }

        if (getScrollX() >= textWidth) {
            repeatCount++;
            if (repeatCount < REPEAT) {
                currentScrollX = -getWidth();
                scrollTo(currentScrollX, 0);
            } else {
                scrollTo(0, 0);
                //
                this.setEllipsize(TruncateAt.END);
                isStop = true;
                return;
            }
        }
        postDelayed(this, 30);
    }

    // 开始滚动
    public void startScroll() {
        isStop = false;
        this.removeCallbacks(this);
        post(this);
    }

    // 停止滚动
    public void stopScroll() {
        isStop = true;
    }

    public void cancel() {
        isStop = true;
        this.removeCallbacks(this);
    }

}
